import pandas as pd
from wordcloud import WordCloud
import matplotlib.pyplot as plt
import logging

def clean_data(data):
    """清洗数据，移除特殊字符"""
    cleaned_data = []
    for row in data:
        cleaned_row = [item.replace('\xa0', '') for item in row]  # 移除特殊字符
        cleaned_data.append(cleaned_row)
    return cleaned_data

def generate_excel(data, excel_filename):
    """生成 Excel 文件"""
    df = pd.DataFrame(data, columns=["排名", "学校名称", "城市", "类型", "总分"])
    try:
        df.to_excel(excel_filename, index=False, engine='openpyxl')
        logging.info(f"Excel 文件 {excel_filename} 已成功生成。")
    except Exception as e:
        logging.error(f"生成 Excel 文件时发生错误：{e}")

def generate_wordcloud(data):
    """生成词云图"""
    text = ' '.join([' '.join(row) for row in data])  # 将二维列表转换为一维字符串
    wordcloud = WordCloud(font_path='simhei.ttf').generate(text)  # 使用宋体字体
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis("off")
    plt.show()

def remove_duplicates(data):
    """对数据进行去重处理"""
    unique_data = []
    seen = set()
    for row in data:
        key = tuple(row)  # 将列表转换为不可变的元组，以便作为字典的键
        if key not in seen:
            unique_data.append(row)
            seen.add(key)
    return unique_data

def filter_by_keyword(data, keyword):
    """根据关键词过滗数据"""
    filtered_data = [row for row in data if keyword in ' '.join(row)]
    return filtered_data

def search_by_rank(data, rank):
    """根据排名搜索数据"""
    rank = str(rank)
    result = [row for row in data if row[0] == rank]
    return result
